<?php
/**
 * 订单表
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/22
 * Time: 14:11
 */

namespace Resource\Model;
use Think\Model;

class OrderModel extends Model
{
    protected $trueTableName = 'wz_order';
    /**
     * 添加
     * @param $params
     * @return mixed
     */
    public function addOrder($params) {
        $params['status'] = 1;
        $params['create_time'] = time();
        $params['update_time'] = time();
        return $this->add($params);
    }

    /**
     * 查询信息
     * @param $params
     * @param string $fields
     * @return mixed|Model
     */
    public function getOrderInfo($params, $fields = '') {
        if($fields) {
            $dataInfo = $this->where($params)->field($fields)->find();
        } else {
            $dataInfo = $this->where($params)->find();
        }
        return $dataInfo;
    }

    /**
     * 查询数量
     * @param
     */
    public function getOrderCount($params) {
        return $this->where($params)->count();
    }

    /**
     * 查询列表
     * @param $params
     * @param int $page
     * @param int $page_size
     * @return array
     */
    public function getOrderList($params, $page=1, $page_size=20) {
        $dataList = $this->where($params)->order('update_time DESC,status ASC')->limit($page_size)->page($page)->select();
        return $dataList ? $dataList : [];
    }

    /**
     * 更新信息
     * @param $params
     * @return bool
     */
    public function updateOrderInfo($params) {
        $params['update_time'] = time();
        $result = $this->save($params);
        if($result !== false) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param $params
     * @return array
     */
    public function getAllOrderList($params) {
        $dataList = $this->where($params)->order('update_time DESC,status ASC')->select();
        return $dataList ? $dataList : [];
    }


    /**
     * 联表查询数量--业支
     * @param
     */
    public function getJoinCount($params=array()) {
        $count = $this->alias('o')
            ->field('o.*,c.company_name as customer_name,c.product_name,d.name as department_name')
            ->join('INNER JOIN __WZ_CUSTOMERS__ as c ON o.customer_id=c.id INNER JOIN __TB_AUTH_USER__ as u ON o.admin_id=u.id INNER JOIN __TB_AUTH_DEPARTMENT__ as d ON u.department_id=d.id')
            ->where($params)
            ->count();
        return $count;
    }

    /**
     * 联表查询--业支
     * @param $params
     * @return array
     */
    public function getJoinOrderList($params=array(),$page=1,$pagesize=20) {
        $dataList = $this->alias('o')
                        ->field('o.*,c.company_name as customer_name,c.product_name,d.name as department_name')
                        ->join('LEFT JOIN __WZ_CUSTOMERS__ as c ON o.customer_id=c.id LEFT JOIN __TB_AUTH_USER__ as u ON o.admin_id=u.id LEFT JOIN __TB_AUTH_DEPARTMENT__ as d ON u.department_id=d.id')
                        ->where($params)
                        ->order('o.update_time DESC,o.status ASC')
                        ->page($page,$pagesize)
                        ->select();
        return $dataList;
    }

    /**
     * 查询一条
     * @param $params
     * @return array
     */
    public function getOneOrderInfo($params){
        $result = $this->where($params)->order('create_time')->limit(1)->select();
        return $result?$result[0]:[];
    }
    /**
     * 根据公司查询订单总数
     * @param $params
     * @return mixed
     */
    public function getOrderCountByCompany($params) {
        return $this->where($params)->order('update_time DESC')->count();
    }

    /**
     * 根据公司查询订单列表
     * @param $params
     * @param int $page
     * @param int $page_size
     * @return array
     */
    public function getOrderListByCompany($params, $page=1, $page_size=20) {
        $dataList = $this->where($params)->order('update_time DESC')->limit($page_size)->page($page)->select();
        return $dataList ? $dataList : [];
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getStatOrderInfo($params){
        $result = $this->where($params)->field('SUM(amount) as total_amount, SUM(base_price) as total_base_price, SUM(profit_price) as total_profit_price, SUM(other_price) as total_other_price')->select();
        return $result;
    }
}